/**
 * 
 */
package com.lanswon.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanswon.common.enums.BindType;
import com.lanswon.dao.UserCardMapper;
import com.lanswon.entity.UserCard;
import com.lanswon.entity.UserVO;
import com.lanswon.qzsmk.service.commonservice.CardProductListVo;
import com.lanswon.qzsmk.service.commonservice.ResultObject;
import com.lanswon.qzsmk.service.commonservice.ResultVo;
import com.lanswon.qzsmk.service.commonservice.impl.DockingInternetService;

/**
 * @author lanswon
 *
 */
@Service
public class UserCardService {
	@Autowired
	private UserCardMapper userCardMapper;
	@Autowired
	private CoreService coreService;
	@Autowired
	private DockingInternetService dockingInternetService;

	public UserCard getUserCardByCardNo(String cardNo) {
		return userCardMapper.selectUserCardByCardNo(cardNo);
	}

	public void addUserCard(UserCard newuc) {
		newuc.setCreateTime(new Date());
		userCardMapper.insert(newuc);
	}

	public List<UserCard> getUserCardByIdNo(String idNum) {
		return userCardMapper.selectUserCardByIdNo(idNum);
	}

	public List<CardProductListVo> isCardBinded(UserVO user, Integer isBinded, String paperNo,String paperType)throws Exception {
		ResultVo resultVo = dockingInternetService.queryCardProdunctByPersonal(paperType, paperNo, "");
		List<CardProductListVo> bindList = new ArrayList<>();
		List<CardProductListVo> unbindList = new ArrayList<>();
		if (resultVo.getResultStatus().getCode() != 200) {
			throw new Exception(resultVo.getResultStatus().getMessage());
		}else{
			ResultObject ro = (ResultObject) resultVo.getServiceObject();
			List<Object> items = ro.getItems();//所有的
			List<UserCard> ucList = getUserCardByIdNo(user.getIdNum());//已绑定
			out:
			for (Object obj : items) {
				CardProductListVo cpListVo = (CardProductListVo) obj;
				for (UserCard uc : ucList) {
					if(uc.getCardNo().equals(cpListVo.getCitizenCardNo())){
						bindList.add(cpListVo);
						break out;
					}
				}
				unbindList.add(cpListVo);
			}
		}
		if(BindType.UNBINDED.getCode()==isBinded){
			return unbindList;
		}else{
			return bindList;
		}
		
		
	}

	public void deleteUserCarByIdNo(String idNo) {
		userCardMapper.deleteUserCarByIdNo(idNo);
	}
	

	

}
